﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Controls_Template_ThongTinHamDiet : System.Web.UI.UserControl
{
    int SIZE_PART = 2;
    class_database db;

    protected void Page_Load(object sender, EventArgs e)
    {
        string idMau = Convert.ToString(Session["idMau"]);
        for (int i = 1; i <= SIZE_PART; i++)
        {
            LoadData(i, idMau);
        }
        if (!IsPostBack)
        {
            CKFinder.FileBrowser _FileBrowser = new CKFinder.FileBrowser();
            _FileBrowser.BasePath = "ckfinder";
            _FileBrowser.SetupCKEditor(CKEditor1);
        }
        SetDisplayEditButton();
        SetTitle(idMau);
    }

    protected void SetDisplayEditButton()
    {
        string sessionUserType = Session["user_type"].ToString();
        string sessionUser = Session["username"].ToString();
        string idMau = Session["idMau"].ToString();
        string nguoiPhanTich = GetNguoiPhanTich(idMau);

        if (sessionUserType == "0" || nguoiPhanTich.ToLower() == sessionUser)
        {
            btnEditThongTinHamDiet.Attributes.Add("style", "display: inline-block");
        } 
    }

    protected string GetNguoiPhanTich(string idMau)
    {
        string sql = "SELECT tbl_user.name FROM tbl_mau, tbl_user WHERE tbl_mau.nguoiphantich = tbl_user.id AND tbl_mau.id = " + idMau;
        db = new class_database();
        DataTable dt = db.GetDataTable(sql);
        string nameNguoiPhanTich = "";
        if (dt != null && dt.Rows.Count > 0)
        {
            nameNguoiPhanTich = dt.Rows[0]["name"].ToString();
        }
        return nameNguoiPhanTich;
    }

    protected void SetTitle(string idMau)
    {
        string sql = "SELECT tbl_mau.tenhamdiet, tbl_mau.date, tbl_user.name FROM tbl_mau, tbl_user WHERE tbl_mau.nguoiphantich = tbl_user.id AND tbl_mau.id = " + idMau;
        db = new class_database();
        DataTable dt = db.GetDataTable(sql);
        if (dt != null && dt.Rows.Count > 0)
        {
            lblNguoiPhanTich.Text = "Người phân tích: " + dt.Rows[0]["name"].ToString();
            lblNgay.Text = "Ngày: " + dt.Rows[0]["date"].ToString();
            lblTenHamDiet.Text = "Tên hàm diệt: " + dt.Rows[0]["tenhamdiet"].ToString();
        }
    }

    private void LoadData(int typePart, string idMau)
    {
        string sql = "SELECT id, noidung, thongtin FROM tbl_tthd_detail WHERE idmau = " + idMau + " AND typeid = " + typePart;
        db = new class_database();
        DataTable dt = db.GetDataTable(sql);
        if (dt != null && dt.Rows.Count > 0)
        {
            if (typePart == 1)
            {
                gridNhanDien.DataSource = dt;
                gridNhanDien.DataBind();
            }
            else if (typePart == 2)
            {
                gridXuLy.DataSource = dt;
                gridXuLy.DataBind();
            }
        }
    }

    protected void btnEditThongTinHamDiet_Click(object sender, EventArgs e)
    {
        gridNhanDien.Columns[3].Visible = true;
        divSubmitResetNhanDien.Attributes.Add("style", "display: block");
        gridXuLy.Columns[3].Visible = true;
        divSubmitResetXuLy.Attributes.Add("style", "display: block");
    }

    protected void rptItemDataBond(object sender, RepeaterItemEventArgs e)
    {

    }

    protected void btnOK_Click(object sender, EventArgs e)
    {
        
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {

    }














    protected void btnSubmitNhanDien_Click(object sender, EventArgs e)
    {
        if (tbNoiDung1.Text == "" && tbThongTin1.Text == "")
        {
            class_database.Show("Không để trống cả 2 ô !!!");
        }
        else
        {
            db = new class_database();
            string[] _param = { "typeid", "noidung", "thongtin", "idmau" };
            string idMau = Convert.ToString(Session["idMau"]);
            string[] _value = { "1", tbNoiDung1.Text, tbThongTin1.Text, idMau };
            string[] _type = { "int", "string", "string", "int" };
            db.InsertRecord("tbl_tthd_detail", _param, _value, _type);
            tbNoiDung1.Text = "";
            tbThongTin1.Text = "";
            LoadData(1, idMau);
        }
    }

    protected void btnResetNhanDien_Click(object sender, EventArgs e)
    {
        tbNoiDung1.Text = "";
        tbThongTin1.Text = "";
    }

    protected void gridNhanDien_RowCancelling(object sender, GridViewCancelEditEventArgs e)
    {
        gridNhanDien.EditIndex = -1;
        string idMau = Convert.ToString(Session["idMau"]);
        LoadData(1, idMau);
    }

    protected void gridNhanDien_RowCommand(object sender, GridViewCommandEventArgs e)
    {

    }

    protected void gridNhanDien_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Label lblIDNhanDien = gridNhanDien.Rows[e.RowIndex].FindControl("lbIDNhanDien") as Label;
        db = new class_database();
        string _where = "id = " + lblIDNhanDien.Text.ToString();
        db.DeleteRecord("tbl_tthd_detail", _where);
        string idMau = Convert.ToString(Session["idMau"]);
        LoadData(1, idMau);
    }

    protected void gridNhanDien_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gridNhanDien.EditIndex = e.NewEditIndex;
        string idMau = Convert.ToString(Session["idMau"]);
        LoadData(1, idMau);
    }

    protected void gridNhanDien_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        Label lbIDNhanDien = gridNhanDien.Rows[e.RowIndex].FindControl("lbIDNhanDien") as Label;
        TextBox tbNoiDungNhanDien = gridNhanDien.Rows[e.RowIndex].FindControl("tbNoiDungNhanDien") as TextBox;
        TextBox tbThongTinNhanDien = gridNhanDien.Rows[e.RowIndex].FindControl("tbThongTinNhanDien") as TextBox;
        string idMau = Convert.ToString(Session["idMau"]);
        db = new class_database();
        string[] _param = { "noidung", "thongtin" };
        string[] _value = { tbNoiDungNhanDien.Text.ToString(), tbThongTinNhanDien.Text.ToString() };
        string _where = "id = " + lbIDNhanDien.Text;
        db.UpdateRecord("tbl_tthd_detail", _param, _value, _where);
        gridNhanDien.EditIndex = -1;
        LoadData(1, idMau);
    }















    protected void btnSubmitXuLy_Click(object sender, EventArgs e)
    {
        if (tbNoiDung2.Text == "" && tbThongTin2.Text == "")
        {
            class_database.Show("Không để trống cả 2 ô !!!");
        }
        else
        {
            db = new class_database();
            string[] _param = { "typeid", "noidung", "thongtin", "idmau" };
            string idMau = Convert.ToString(Session["idMau"]);
            string[] _value = { "2", tbNoiDung2.Text, tbThongTin2.Text, idMau };
            string[] _type = { "int", "string", "string", "int" };
            db.InsertRecord("tbl_tthd_detail", _param, _value, _type);
            tbNoiDung2.Text = "";
            tbThongTin2.Text = "";
            LoadData(2, idMau);
        }
    }

    protected void btnResetXuLy_Click(object sender, EventArgs e)
    {
        tbNoiDung2.Text = "";
        tbThongTin2.Text = "";
    }

    protected void gridXuLy_RowCancelling(object sender, GridViewCancelEditEventArgs e)
    {
        gridXuLy.EditIndex = -1;
        string idMau = Convert.ToString(Session["idMau"]);
        LoadData(2, idMau);
    }

    protected void gridXuLy_RowCommand(object sender, GridViewCommandEventArgs e)
    {

    }

    protected void gridXuLy_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Label lblIDXuLy = gridXuLy.Rows[e.RowIndex].FindControl("lbIDXuLy") as Label;
        db = new class_database();
        string _where = "id = " + lblIDXuLy.Text.ToString();
        db.DeleteRecord("tbl_tthd_detail", _where);
        string idMau = Convert.ToString(Session["idMau"]);
        LoadData(2, idMau);
    }

    protected void gridXuLy_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gridXuLy.EditIndex = e.NewEditIndex;
        string idMau = Convert.ToString(Session["idMau"]);
        LoadData(2, idMau);
    }

    protected void gridXuLy_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        Label lbIDXuLy = gridXuLy.Rows[e.RowIndex].FindControl("lbIDXuLy") as Label;
        TextBox tbNoiDungXuLy = gridXuLy.Rows[e.RowIndex].FindControl("tbNoiDungXuLy") as TextBox;
        TextBox tbThongTinXuLy = gridXuLy.Rows[e.RowIndex].FindControl("tbThongTinXuLy") as TextBox;
        string idMau = Convert.ToString(Session["idMau"]);
        db = new class_database();
        string[] _param = { "noidung", "thongtin" };
        string[] _value = { tbNoiDungXuLy.Text.ToString(), tbThongTinXuLy.Text.ToString() };
        string _where = "id = " + lbIDXuLy.Text;
        db.UpdateRecord("tbl_tthd_detail", _param, _value, _where);
        gridXuLy.EditIndex = -1;
        LoadData(2, idMau);
    }
}